from typing import List

from dao.player_dao import PlayerDao
from model.player_model import PlayerModel

player_dao = PlayerDao()


class PlayerService:
    def create(self, model: PlayerModel) -> int:
        """
        创建
        """
        result = player_dao.get_by_name(model.name)
        if not result:
            return player_dao.create(model)

    def update(self, model: PlayerModel) -> int:
        """
        更新
        """
        if player_dao.get(model.id):
            player_dao.update(model)
        return model.id

    def delete(self, id: int) -> int:
        """
        删除
        """
        if self.get(id):
            return player_dao.delete(id)

    def list(self) -> List[PlayerModel]:
        """
        获取全部
        """
        return player_dao.list()

    def get(self, id: int) -> PlayerModel:
        """
        获取某个数据
        """
        return player_dao.get(id)

    def get_by_name(self, name) -> PlayerModel:
        """
        根据名称获取某个数据
        """
        return player_dao.get_by_name(name=name)